/*
 * data.c
 *
 *  Created on: 16/04/2012
 *      Author: chelo
 */

#include "../collections.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>



def def_create(int TKey, int TValue, void* key, void* value, def link)
{
	def self = malloc(sizeof(struct node) + TKey + TValue);
	def_write(self, TKey, TValue, key, value);
	def_connect(self, link);
	return self;
}

void def_writeKey(def self, int TKey, void* key)
{
	memcpy((void*) self + sizeof(void*), (const void*) (void*) key, TKey);
}

void def_writeValue(def self, int TKey, int TValue, void* value)
{
	memcpy((void*) self + sizeof(void*) + TKey, (const void*) (void*) value, TValue);
}

void def_write(def self, int TKey, int TValue, void* key, void* value)
{
	def_writeKey(self,TKey,key);
	def_writeValue(self,TKey,TValue,value);
}

def def_delete(def self)
{
	free(self);
	self = NULL;

	return self;
}

void def_readKey(def self, int TKey, void * key)
{
	memcpy(key, (const void *) (void*) self + sizeof(void*), TKey);
}

void def_readValue(def self, int TKey, int TValue, void * value)
{
	memcpy(value, (const void *) (void*) self + sizeof(void*) + TKey, TValue);
}

int def_read(def self, int TKey, int TValue, void * key, void * value)
{
	def_readKey(self,TKey,key);
	def_readValue(self,TKey,TValue,value);

	return (key != NULL && value != NULL);
}

int def_isKey(def self, int TKey, void * key)
{
	return memcmp(key, (const void *) (void*) self + sizeof(void*), TKey) == 0;
}

int def_isValue(def self, int TKey, int TValue, void * value)
{
	return memcmp(value, (const void *) (void*) self + sizeof(void*) + TKey, TValue) == 0;
}

int def_isDef(def self, int TKey, int TValue, void * key, void * value)
{
		return (def_isKey(self,TKey,key)
	            && def_isValue(self,TKey,TValue,value));
}

void def_connect(def self, def link)
{
	self->link = link;
}

def def_getlink(def self)
{
	return self->link;
}
